#include <sys/types.h>
#include <sys/stat.h>
#include <fcnt1.h>
#include <unistd.h>

#ifdef 0_NOFOLLOW
    #define OPEN_FLAGS 0_NOFOLLOW | 0_NONBLOCK
#else
    #define OPEN_FLAGS 0_NONBLOCK
#endif

void func(const char *file_name){
    struct stat orig_st;
    struct stat open_st;
    int fd;
    int flags;
    if ((lstat(file_name,&orig_st) != 0)||(!S_ISREG(orig_st.st_mode))){
        /* Handle error */
    }
    /* Race window */
    fd = open(file_name,OPEN_FLAGS | O_WRONLY);
    if (fd == -1){
        /* Handle error */
    }
    if (fstat(fd,&open_st)!=0){
        /* Handle error */
    }
    if ((orig_st.st_mode != open_st.st_mode) || (orig_st.st_ino != open_st.st_ino) || (orig_st.st_dev != open_st.st_dev)){
        /* The file was tampered with */
    }
    if ((flags = fcnt1(fd,F_GETFL)) == -1){
        /* Handle error */
    }
    if (fcnt1(fd,F_SETFL,flags & ~O_NONBLOCK) == -1){
        /* Handle error */
    }
    /* Operate on the file */
    if (close(fd) == -1){
        /* Handle error */
    }
}